﻿using System;

namespace JTLeigh.Commerce.Spencer.Features.ProductFeedback.Business
{

	/// <summary>
	/// Counter used to calculate the average of a series of values.
	/// </summary>
	public class AveragingCounter
	{

		#region Fields

		private long _sum;
		private int _count;

		#endregion // Fields

		#region Properties

		/// <summary>
		/// Gets the sum of the values that have been added.
		/// </summary>
		public long Sum
		{
			get { return _sum; }
		}

		/// <summary>
		/// Gets the count of the values that have been added.
		/// </summary>
		public int Count
		{
			get { return _count; }
		}

		/// <summary>
		/// Gets the average of the values that have been added.
		/// </summary>
		/// <value>The average <see cref="double"/> value, or <see langword="null"/> if no values 
		/// have been added.</value>
		public double? Average
		{
			get { return (_count > 0) ? (double?)((double)_sum / (double)_count) : null; }
		}

		#endregion // Properties

		#region Methods

		/// <summary>
		/// Adds a value.
		/// </summary>
		/// <param name="value">The <see cref="Byte"/> value to add.</param>
		public virtual void AddValue(byte value)
		{
			_sum += value;
			_count++;
		}

		/// <summary>
		/// Resets the counter.
		/// </summary>
		public virtual void Reset()
		{
			_sum = 0;
			_count = 0;
		}

		#endregion // Methods

	}

}
